package com.eralink.unionuhfreader.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.eralink.unionuhfreader.R;
import com.eralink.unionuhfreader.bean.ProductVo;
import com.eralink.unionuhfreader.util.ToastUtil;

import java.util.List;

public class ProductRecycleViewAdapter extends RecyclerView.Adapter<ProductRecycleViewAdapter.ViewHold>{

    private List<ProductVo> mList;//数据源
    private Context mContext;
    private ManualCateRecycleViewAdapter.CalCount calCount;
    public void setCalCount(ManualCateRecycleViewAdapter.CalCount c){
        calCount=c;
    }
    public  ProductRecycleViewAdapter(Context context,List<ProductVo> vos){
        this.mContext=context;
        this.mList=vos;
    }
    @NonNull
    @Override
    public ViewHold onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_product, parent, false);
        //将view传递给我们自定义的ViewHolder
        ViewHold holder = new ViewHold(view);
        //返回这个MyHolder实体
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHold holder, int position) {
            holder.tvName.setText(mList.get(position).getProductName());
            holder.tvMoney.setText(mList.get(position).getSalePrice()+"");
            holder.tv_single_counts.setText(mList.get(position).getCount()+"");
        holder.tx_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (calCount!=null){
                    calCount.addOrDivClick(position,1);
                }
            }
        });
        holder.tx_jian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (calCount!=null){

                    if (mList.get(position).getCount()==1){
                        ToastUtil.showToast("商品数量不能低于1");
                        return;
                    }
                    calCount.addOrDivClick(position,-1);
                }
            }
        });
        holder.tx_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (calCount!=null){
                    calCount.addOrDivClick(position,0);
                }
            }
        });
        }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class ViewHold extends RecyclerView.ViewHolder{
        private TextView tvMoney;
        private TextView tvName;
        private TextView tv_single_counts,tx_del,tx_jian,tx_add;;
        public ViewHold(View convertView) {
            super(convertView);
            tvName = (TextView) convertView.findViewById(R.id.name);
            tvMoney = (TextView) convertView.findViewById(R.id.price);

            tv_single_counts=convertView.findViewById(R.id.tv_single_counts);
            tx_jian=convertView.findViewById(R.id.tx_jian);
            tx_add=convertView.findViewById(R.id.tx_add);
            tx_del=convertView.findViewById(R.id.tx_del);
        }
    }
}
